package el.util.force;

import prefuse.util.force.Force;
import prefuse.util.force.ForceItem;
import prefuse.util.force.ForceSimulator;
import prefuse.util.force.Integrator;
import prefuse.util.force.RungeKuttaIntegrator;

public class ForceSimulatorWhole extends ForceSimulator{
	    private Force[] wforces;
	    private int wlen;
	    /**
	     * Create a new, empty ForceSimulator. A RungeKuttaIntegrator is used
	     * by default.
	     */
	    public ForceSimulatorWhole() {
	        this(new RungeKuttaIntegrator());
	    }

	    /**
	     * Create a new, empty ForceSimulator.
	     * @param integr the Integrator to use
	     */
	    public ForceSimulatorWhole(Integrator integr) {
	    	super(integr);
	        wforces = new Force[5];
	        wlen = 0;
	    }

	    /**
	     * Add a new Force function to the simulator.
	     * @param f the Force function to add
	     */
	    public void addForce(Force f) {	        
	        super.addForce(f);
	        //Careful
	        if ((f.isItemForce()==false)&&(f.isSpringForce()==false)){
	        	if ( wforces.length == wlen ) {
	                // resize necessary
	                Force[] newf = new Force[wlen+10];
	                System.arraycopy(wforces, 0, newf, 0, wforces.length);
	                wforces = newf;
	            }
	        	wforces[wlen++] = f;
	        }
	    }
	    
	    /**
	     * Get an array of all the Force functions used in this simulator.
	     * @return an array of Force functions
	     */
	    public Force[] getForces() {
	        Force[] previous = super.getForces();
	        Force[] rv = new Force[previous.length + wlen];
	        System.arraycopy(previous, 0, rv, 0, previous.length);
	        System.arraycopy(wforces, 0, rv, previous.length , wlen);
	        return rv;
	    }
	    
	    
	    /**
	     * Accumulate all forces acting on the items in this simulation
	     */
	    public void accumulate() {
	        super.accumulate();	        
	        for ( int i = 0; i < wlen; i++ )
	            wforces[i].init(this);
	        for (int i=0; i<wlen; i++){
	        	wforces[i].getForce((ForceItem)null);
	        }
	    }
	
}
